import { ShopSearchPage } from './../../pages/shop-search/shop-search';
import { Component, Injector } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL } from "./../../providers/Config";


/**
 * Generated class for the FollowShopComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'follow-shop',
  templateUrl: 'follow-shop.html'
})
export class FollowShopComponent extends BasePage {

  picrooturl = "";//图片服务地址
  userinfo = {}//获取内存中的数据
  focuselist = [];//关注过的店铺
  flag1: boolean;
  door = '';
  store = ''
  a1: boolean;
  a2: boolean;
  a3: boolean
  constructor(injector: Injector, public userService: UserService, public stc: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL
    this.userinfo = this.stc.read('userinfo');

    this.door = "上门";
    this.store = "到店";
    this.getUserFocusAPI();


  }
  // ionViewDidLoad(){
  //   this.getUserFocusAPI();
  // }
/**
   * 选择要关注哪些美甲师
   * @param i   哪一个
   * @param j   关注还是取消关注   1是已关注  0是取消关注
   */
  chose(i,j){
    let id=[];
    i['focused']=j;
    console.log(this.focuselist)
    this.focuselist.forEach((e)=>{
      if(e.focused == 0){
        id.push(e.shop_id);
      }
    })
    console.log(id);
    this.stc.write("focuse2",id);
  }



  /**取消关注 */
  // async focusAPI(item) {
  //   let param = { "focused_id": item['shop_id'], 'user_id': this.userinfo['user_id'] };
  //   let loader = this.loadingCtrl.create({
  //     content: "",
  //   });
  //   let result = await this.userService.focusAPI(param);
  //   loader.dismiss();
  //   if (result.code == 1) {
  //     this.getUserFocusAPI();
  //   } else {      
  //     this.showAlert(result.msg);
  //     this.getUserFocusAPI();
  //   }
  // }

  /**获取关注列表 */
  async getUserFocusAPI() {
    let param = { 'focus_type': '2', 'user_id': this.userinfo['user_id'] };
    let loader = this.loadingCtrl.create({
      content: "",
    });
    let result = await this.userService.getUserFocusAPI(param);
    loader.dismiss();
    console.log(result)
    if (result.code == 1) {
      if(result.data.list.length != 0) {
        this.flag1 = true;
        this.focuselist = result.data.list;
        this.focuselist.forEach((e)=>{
          e['focused']=1
        })
      }else{
        this.flag1 = false;
      }
    }else if(result.code == 1002){
        this.flag1 = false;
        console.log("没关注任何美甲店铺")
    }else{
      this.flag1 = false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }
  home(){
    this.navCtrl.push('shopSearch');
  }
  //跳转到美甲店详情
  gotoShopDetail(item,e){
    if(e.target.className == 'focusBtn2'){
      return false;
    }else{
      let modal = this.modalCtrl.create("NaiShopDetailPage", {shop_id:item.shop_id});
      modal.onDidDismiss(data => {

      });
      modal.present();
    }
  }
}
